/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.ProbeCache;
import com.sinodata.bsm.center.cache.ProtocolParameterCache;
import com.sinodata.bsm.center.message.Messenger;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.ProtocolConstants;
import com.sinodata.bsm.common.constants.SystemStatus;
import com.sinodata.bsm.common.message.ServiceMessage;
import com.sinodata.bsm.common.utils.Encipher;
import com.sinodata.bsm.common.vo.Probe;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-8 下午9:18:04          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ConfigConnAction extends BaseAction<ProtocolParameter> {

    /**
     * 
     */
    private static final long serialVersionUID = -7327872700775808559L;
    @Autowired
    private ProbeCache probeCache;
    @Autowired
    private ProtocolParameterCache protocolParameterCache;

    private Long id;
    private String type;
    private String ip;
    private Integer port;
    private String userName;
    private String pwd;
    private String prompt;
    private String protocol;
    private String instName;
    private String url;
    private String instType;
    private Integer maxConn;

    public String common() {
        Struts2Utils.setRequestAttribute("id", request.getParameter("id"));
        Struts2Utils.setRequestAttribute("type", request.getParameter("type"));
        return "common";
    }

    public String allProbe() {
        List<Probe> listResult = new ArrayList<Probe>();
        List<Probe> listProbe = probeCache.getAll();
        for (Probe probe : listProbe) {
            if (probe.getStatus() == SystemStatus.STATUS_RUN) {
                listResult.add(probe);
            }
        }
        Struts2Utils.renderJson(listResult);
        return null;
    }

    public String connTest() {
        String type = request.getParameter("type");
        String probeId = request.getParameter("propId");
        ProtocolParameter protocolParameter = new ProtocolParameter();
        protocolParameter.setId(id);
        protocolParameter.setInstName(instName);
        protocolParameter.setInstType(instType);
        protocolParameter.setIp(ip);
        protocolParameter.setMaxConn(maxConn);
        protocolParameter.setPort(port);
        protocolParameter.setPrompt(prompt);
        protocolParameter.setProtocol(protocol);
        protocolParameter.setType(type);
        protocolParameter.setUserName(userName);
        protocolParameter.setPwd(pwd);
        if (protocolParameter.getId() != null) {
            if (protocolParameter.getPwd() == null || protocolParameter.getPwd().equals("")) {
                protocolParameter.setPwd(Encipher.DecodePasswd(protocolParameterCache.get(protocolParameter.getId()).getPwd()));
            }
        }
        //测试连接
        ServiceMessage message = new ServiceMessage();
        //数据库配置测试
        if (type.equals(ProtocolConstants.TYPE_JDBC)) {
            if (instType != null && instType.equals("oracle")) {
                protocolParameter.setUrl("jdbc:oracle:thin:@$ip:$port:$dbname");
            }
            if (instType != null && instType.equals("db2")) {
                protocolParameter.setUrl("jdbc:db2://$ip:$port/$dbname");
            }
            if (instType != null && instType.equals("sybase")) {
                protocolParameter.setUrl("jdbc:sybase:Tds:$ip:$port");
            }
            if (instType != null && instType.equals("sqlserver")) {
                protocolParameter.setUrl("jdbc:sqlserver://$ip:$port");
            }
            if (instType != null && instType.equals("mysql")) {
                protocolParameter.setUrl("jdbc:mysql://$ip:$port/$dbname");
            }
            if (instType != null && instType.equals("informix")) {
                protocolParameter.setUrl("jdbc:informix-sqli://$ip:$port/sysmaster:INFORMIXSERVER=$dbname");
            }

            message.setSrvId("DBConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_JMX)) {
            message.setSrvId("JmxConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_HOST)) {
            message.setSrvId("HostConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_WMI)) {
            message.setSrvId("WmiConnectionService");
        }
        message.setMethod("testConn");
        if (type.equals(ProtocolConstants.TYPE_HOST)) {
            message.setParams(new Object[] { protocolParameter, "1" });
        } else {
            message.setParams(new Object[] { protocolParameter });
        }
        Object ob = null;
        boolean flag = true;
        try {
            ob = Messenger.getInstance().sendSyncMessage(Long.valueOf(probeId), message);
        } catch (Exception e) {
            flag = false;
        } finally {
            if (ob != null) {
                flag = Boolean.valueOf(String.valueOf(ob));
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("flag", String.valueOf(flag));
            map.put("ip", protocolParameter.getIp());
            Struts2Utils.renderJson(map);
        }
        return null;
    }

    public String test() {
        String type = request.getParameter("type");
        String id = request.getParameter("id");
        String probeId = request.getParameter("propId");

        ProtocolParameter ppCache = protocolParameterCache.get(Long.valueOf(id));
        ProtocolParameter protocolParameter = new ProtocolParameter();
        protocolParameter.setId(ppCache.getId());
        protocolParameter.setInstName(ppCache.getInstName());
        protocolParameter.setInstType(ppCache.getInstType());
        protocolParameter.setIp(ppCache.getIp());
        protocolParameter.setMaxConn(ppCache.getMaxConn());
        protocolParameter.setPort(ppCache.getPort());
        protocolParameter.setPrompt(ppCache.getPrompt());
        protocolParameter.setProtocol(ppCache.getProtocol());
        protocolParameter.setType(ppCache.getType());
        protocolParameter.setUrl(ppCache.getUrl());
        protocolParameter.setUserName(ppCache.getUserName());
        protocolParameter.setPwd(Encipher.DecodePasswd(ppCache.getPwd()));

        ServiceMessage message = new ServiceMessage();
        //数据库配置测试
        if (type.equals(ProtocolConstants.TYPE_JDBC)) {
            message.setSrvId("DBConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_JMX)) {
            message.setSrvId("JmxConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_HOST)) {
            message.setSrvId("HostConnectionService");
        } else if (type.equals(ProtocolConstants.TYPE_WMI)) {
            message.setSrvId("WmiConnectionService");
        }
        message.setMethod("testConn");
        if (type.equals(ProtocolConstants.TYPE_HOST)) {
            message.setParams(new Object[] { protocolParameter, "0" });
        } else {
            message.setParams(new Object[] { protocolParameter });
        }
        Object ob = null;
        boolean flag = true;
        try {
            ob = Messenger.getInstance().sendSyncMessage(Long.valueOf(probeId), message);
        } catch (Exception e) {
            flag = false;
        } finally {
            if (ob != null) {
                flag = Boolean.valueOf(String.valueOf(ob));
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("flag", String.valueOf(flag));
            map.put("ip", protocolParameter.getIp());
            Struts2Utils.renderJson(map);
        }
        return null;
    }

    @Override
    public ProtocolParameter getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the port
     */
    public Integer getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(Integer port) {
        this.port = port;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the pwd
     */
    public String getPwd() {
        return pwd;
    }

    /**
     * @param pwd the pwd to set
     */
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    /**
     * @return the prompt
     */
    public String getPrompt() {
        return prompt;
    }

    /**
     * @param prompt the prompt to set
     */
    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }

    /**
     * @return the protocol
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * @param protocol the protocol to set
     */
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    /**
     * @return the instName
     */
    public String getInstName() {
        return instName;
    }

    /**
     * @param instName the instName to set
     */
    public void setInstName(String instName) {
        this.instName = instName;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the instType
     */
    public String getInstType() {
        return instType;
    }

    /**
     * @param instType the instType to set
     */
    public void setInstType(String instType) {
        this.instType = instType;
    }

    /**
     * @return the maxConn
     */
    public Integer getMaxConn() {
        return maxConn;
    }

    /**
     * @param maxConn the maxConn to set
     */
    public void setMaxConn(Integer maxConn) {
        this.maxConn = maxConn;
    }

}
